package com.cuihanyu.newuniversityover.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.internet.GlobalContent;
import com.cuihanyu.newuniversityover.util.MyLogUtil;
import com.dou361.ijkplayer.bean.VideoijkBean;
import com.dou361.ijkplayer.listener.OnShowThumbnailListener;
import com.dou361.ijkplayer.widget.PlayStateParams;
import com.dou361.ijkplayer.widget.PlayerView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017-08-07.
 */
public class VideoActivity extends AppCompatActivity {

    private PlayerView player;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVideoView();
    }

    private void initVideoView() {
        /**播放器*/
        View rootView = getLayoutInflater().from(VideoActivity.this).inflate(R.layout.activity_video, null);
        setContentView(rootView);
        intent = getIntent();
        TextView textView = findViewById(R.id.app_video_title);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.setClass(VideoActivity.this, StudyNoteActivity.class);
                intent.putExtra("Chapter", intent.getStringExtra("Chapter"));
                intent.putExtra("item", intent.getStringExtra("item"));
                startActivity(intent);
            }
        });
        /**播放资源*/
        List<VideoijkBean> list = new ArrayList<>();
        VideoijkBean m1 = new VideoijkBean();
        m1.setStream("标清");
        m1.setUrl(GlobalContent.HOME_Video);
        VideoijkBean m2 = new VideoijkBean();
        m2.setStream("高清");
        m2.setUrl(GlobalContent.HOME_Video);
        list.add(m1);
        list.add(m2);
        /**加载前显示的缩略图*/
        /**加载前显示的缩略图*/player = new PlayerView(VideoActivity.this)
                .setTitle(intent.getStringExtra("item") + "(点击标题写学习笔记)")
                .setScaleType(PlayStateParams.fitparent)
                .hideMenu(true)
                .forbidTouch(false)
                .showThumbnail(new OnShowThumbnailListener() {
                    @Override
                    public void onShowThumbnail(ImageView ivThumbnail) {
                        /**加载前显示的缩略图*/
                        Glide.with(VideoActivity.this)
                                .load(R.drawable.banner1)
                                .into(ivThumbnail);
                    }
                })
                .setPlaySource(list)
                .startPlay();

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
        /**demo 的内容，恢复系统其它媒体的状态*/
        //MediaUtils.muteAudioFocus(mContext, true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
        /**demo 的内容，恢复设备亮度状态*/
        //if (wakeLock != null) {
        //    wakeLock.release();
        //}
    }
}
